package main

import (
	"fmt"
	"net"
)

func process(conn net.Conn) {
	defer conn.Close()
	for {
		buf := make([]byte, 1024)
		fmt.Printf("服务端正在等待客户端%v发消息\n", conn.RemoteAddr())
		n, err := conn.Read(buf) //从conn读取
		if err != nil {
			fmt.Println("客户端退出,err=", err)
			return
		}
		fmt.Print(string(buf[:n]))
	}
}

func main() {
	fmt.Println("服务器开始监听...")
	listen, err := net.Listen("tcp", "10.251.50.46:8888")
	if err != nil {
		fmt.Println("listen err=", err)
		return
	}
	defer listen.Close()
	for {
		fmt.Println("等待客户端来链接...")
		conn, err := listen.Accept()
		if err != nil {
			fmt.Println("Acccept() err=", err)
		} else {
			fmt.Printf("Accrpt() suc con=%v 客户端ip=%v\n", conn, conn.RemoteAddr())
		}
		go process(conn)
	}
}
